﻿using Study.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp4
{
    /// <summary>
    /// MyTreeView.xaml 的交互逻辑
    /// </summary>
    public partial class MyTreeView : Window
    {
        public MyTreeView()
        {
            InitializeComponent();

            var _menuNodes = new List<TreeViewNode>()
            {
                new TreeViewNode() { Id = 1, Name = "根目录", ParentId = 0 },
                new TreeViewNode() { Id = 2, Name = "财务管理", ParentId = 1 },
                new TreeViewNode() { Id = 3, Name = "教学管理", ParentId = 1 },
                new TreeViewNode() { Id = 4, Name = "数据统计", ParentId = 1 },
                new TreeViewNode() { Id = 5, Name = "订单管理", ParentId = 2 },
                new TreeViewNode() { Id = 6, Name = "结算管理", ParentId = 2 },
                new TreeViewNode() { Id = 7, Name = "课程管理", ParentId = 3 },
                new TreeViewNode() { Id = 8, Name = "作业管理", ParentId = 3 },
                new TreeViewNode() { Id = 9, Name = "课程数据", ParentId = 4 },
                new TreeViewNode() { Id = 10, Name = "粉丝数据", ParentId = 4 },
            };

            #region TreeView方式
            //var rootNode = _menuNodes.Where(x => x.ParentId == 0).FirstOrDefault();
            //TreeViewItem root = new TreeViewItem()
            //{
            //    Uid = rootNode.Id.ToString(),
            //    Header = rootNode.Name,
            //    Tag = rootNode
            //};
            //BuildTree(_menuNodes, root);
            //treeView.Items.Add(root);
            #endregion

            List<TreeNodeViewModel> treeNodeViewModels = new List<TreeNodeViewModel>();
            var rootNode = _menuNodes.Where(x => x.ParentId == 0).FirstOrDefault();
            var root = new TreeNodeViewModel()
            {
                Id = rootNode.Id,
                Name = rootNode.Name
            };
            BuildTreeViewModel(_menuNodes, root);
            treeNodeViewModels.Add(root);
            treeView.ItemsSource = treeNodeViewModels;

        }

        private void BuildTreeViewModel(List<TreeViewNode> _menuNodes, TreeNodeViewModel parentNode)
        {
            foreach (var item in _menuNodes.Where(x => x.ParentId == parentNode.Id))
            {
                TreeNodeViewModel childNode = new TreeNodeViewModel()
                {
                    Id = item.Id,
                    Name = item.Name
                };
                parentNode.Children.Add(childNode);
                BuildTreeViewModel(_menuNodes, childNode);
            }
        }

        /// <summary>
        /// 递归创建树形结构
        /// </summary>
        /// <param name="_menuNodes"></param>
        /// <param name="parentNode"></param>
        private void BuildTree(List<TreeViewNode> _menuNodes, TreeViewItem parentNode)
        {
            foreach (var item in _menuNodes.Where(x => x.ParentId == Convert.ToInt32(parentNode.Uid)))
            {
                TreeViewItem childNode = new TreeViewItem()
                {
                    Uid = item.Id.ToString(),
                    Header = item.Name,
                    Tag = item
                };
                parentNode.Items.Add(childNode);
                BuildTree(_menuNodes, childNode);
            }
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var selectedItem = e.NewValue as TreeViewItem;
            var selectedValue = treeView.SelectedValue;
        }
    }
}
